/*   Shotscreens
 *   Copyright (C) 2010, Vadim Zakondyrin (http://crystalnix.com)
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "qgraphicsbound.h"
#include <QGraphicsRectItem>
#include <QGraphicsScene>
#include <QPen>

QGraphicsBound::QGraphicsBound()
{
    boundedItem = NULL;
}

QGraphicsBound::~QGraphicsBound()
{

}

void QGraphicsBound::show()
{
    boundingRect->setRect(boundedItem->boundingRect());
}

void QGraphicsBound::hide()
{
    boundingRect->setRect(QRectF());
}

void QGraphicsBound::selectItem(QGraphicsItem *selectedItem)
{
    unselectItem();
    boundedItem = selectedItem;

    boundingRect = new QGraphicsRectItem;
    boundingRect->setBrush(QBrush(Qt::NoBrush));
    boundingRect->setPen(QPen(Qt::DashLine));
    boundingRect->setPos(boundedItem->pos());

    selectedItem->scene()->addItem(boundingRect);
    show();
}

void QGraphicsBound::unselectItem()
{
    if (boundedItem != NULL) {
        hide();
        position = QPointF();
        boundedItem->scene()->removeItem(boundingRect);
        delete boundingRect;
        boundedItem = NULL;
    }
}

void QGraphicsBound::startMoving(const QPointF &pos)
{
    position = boundedItem->mapFromScene(pos);
}

void QGraphicsBound::continueMoving(const QPointF &pos)
{
    QPointF newPos = pos - (position - boundedItem->mapFromScene(boundedItem->pos()));
    if(newPos.x()<MoveMaxRect.x()&&newPos.y()<MoveMaxRect.y())
    {

    }
    boundedItem->setPos(newPos);
    boundingRect->setPos(newPos);
}
void QGraphicsBound::SetMoveMaxRect(QRect rect)
{
    MoveMaxRect=rect;
}
